﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TaskProject.Application.Command.Plan;
using TaskProject.ErrorCode;
using TaskProject.Extension.Plan;

namespace TaskProject.Controllers
{
    /// <summary>
    /// 生产计划管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PlanController : ControllerBase
    {
        private readonly IMediator _mediator;

        public PlanController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 新增生产计划
        /// </summary>
        /// <param name="request">生产计划信息</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddProductionPlan([FromBody] AddProductionPlanCommand request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 查询生产计划
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <returns>生产计划列表</returns>
        [HttpGet]
        public async Task<APIResult<List<ProductionPlanDTO>>> GetProductionPlan([FromQuery] GetProductionPlanCommand request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 根据主键查询生产计划
        /// </summary>
        /// <param name="planId">生产计划ID</param>
        /// <returns>生产计划信息</returns>
        [HttpGet("{planId}")]
        public async Task<APIResult<ProductionPlanDTO>> GetProductionPlanById(int planId)
        {
            var request = new GetProductionPlanByIdCommand { PlanId = planId };
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 获取生产计划详情
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <returns>生产计划详情</returns>
        [HttpPost]
        public async Task<APIResult<ProductionPlanDetailDTO>> GetProductionPlanDetail([FromBody] GetProductionPlanDetailCommand request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 更新生产计划
        /// </summary>
        /// <param name="request">更新信息</param>
        /// <returns>更新结果</returns>
        [HttpPost]
        public async Task<APIResult<bool>> UpdateProductionPlan([FromBody] UpdateProductionPlanCommand request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 生产计划操作（启动、暂停、恢复、完成、取消）
        /// </summary>
        /// <param name="request">操作信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIResult<bool>> ProductionPlanOperation([FromBody] ProductionPlanOperationCommand request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 获取生产计划操作记录
        /// </summary>
        /// <param name="planId">生产计划ID</param>
        /// <param name="operationType">操作类型（可选）</param>
        /// <param name="startTime">开始时间（可选）</param>
        /// <param name="endTime">结束时间（可选）</param>
        /// <param name="operatorName">操作人（可选）</param>
        /// <returns>操作记录列表</returns>
        [HttpGet]
        public async Task<APIResult<List<ProductionPlanOperationLogDTO>>> GetProductionPlanOperationLogs(
            [FromQuery] int planId,
            [FromQuery] int? operationType = null,
            [FromQuery] DateTime? startTime = null,
            [FromQuery] DateTime? endTime = null,
            [FromQuery] string? operatorName = null)
        {
            var request = new GetProductionPlanOperationLogsCommand
            {
                PlanId = planId,
                OperationType = operationType,
                StartTime = startTime,
                EndTime = endTime,
                OperatorName = operatorName
            };
            return await _mediator.Send(request);
        }
    }
}
